<?php

require_once './application/core/CoreBase.php';
require_once './application/models/Tables/Utilisateurs.php';
require_once './application/models/Objets/Utilisateur.php';
require_once './application/entity/connexion/listeuser.php';
require_once './application/entity/connexion/listedroit.php';
require_once './application/entity/connexion/listenetwork.php';
require_once './application/entity/connexion/infouser.php';
require_once './application/entity/connexion/infodroit.php';

class ConnexionCore extends ExecCore
{

    private $_user;
    private $_login;
    private $_pass;
    private $_email;
    private $_obj;
    private $_id;
    private $_info;
    private $_droit;
    private $_descDroit;
    private $_idDroit;
    private $_network;
    protected $_session;
    private $_interface;
    private $_key;
    private $_listLang;

    public function getConnexion()
    {
        $this->_login = $this->_controller->getRequest()->getParam('name');
        $this->_pass = $this->_controller->getRequest()->getParam('pwd');
        $this->_login = strtolower($this->_login);
        if (!empty($this->_pass))
            $this->_pass = $this->EncryptedPassword($this->_pass);
        $obj = new ObjectUtilisateur();
        if (isset($this->_login) && trim($this->_login) != "" && isset($this->_pass) && trim($this->_pass))
        {
            $this->_user = $obj->authentification($this->_login, $this->_pass);
            if ($this->_user["idUser"])
            {
                $session = new Zend_Session_Namespace('session');
                $session->idClient = $this->_user["idUser"];
                $session->idDroit = $this->_user["idDroit"];
                $session->lang = $this->_user["Langue"];
                $this->_user = $this->_login;
                $this->getUserSession();
                return true;
            }
        }
        return false;
    }

    private function EncryptedPassword($pass)
    {
        $this->_key = '6fd26cd009ae4a601339a33ccb734bfba96c7e0e6896a640449a4a64a3bb2962';
        return md5(base64_encode($this->_key . $pass));
    }

    public function addUser()
    {
        $this->_login = $this->_controller->getRequest()->getParam('login');
        $this->_pass = $this->_controller->getRequest()->getParam('pass');
        $this->_email = $this->_controller->getRequest()->getParam('email');
        $this->_droit = $this->_controller->getRequest()->getParam('droit');
        $this->_descDroit = $this->_controller->getRequest()->getParam('list');
        $this->_lang = $this->_controller->getRequest()->getParam('lang');
        $this->_login = strtolower($this->_login);
        $this->_droit = strtolower($this->_droit);
        if (!empty($this->_pass))
            $this->_pass = $this->EncryptedPassword($this->_pass);
        $this->_obj = new ObjectUtilisateur;
        $this->_info = $this->_obj->chargeUser($this->_login);
        if (isset($this->_info[0]['IdUtilisateur']))
        {
            echo Zend_Json_Encoder::encode(false);
            return false;
        }
        if ($this->_droit == 'admin' || $this->_droit == 'administrateur')
            $this->_obj->ajoutUtilisateur($this->_login, $this->_pass, $this->_email, '1', $this->_lang);
        else
            $this->_obj->ajoutUtilisateur($this->_login, $this->_pass, $this->_email, $this->_droit, $this->_lang, $this->_descDroit);
        echo Zend_Json_Encoder::encode(true);
    }

    public function InfoUser()
    {
        $this->_login = $this->_controller->getRequest()->getParam('login');
        $this->_login = strtolower($this->_login);
        $this->_obj = new ObjectUtilisateur;
        if ($this->_login == "null")
        {
            $this->_login = new Zend_Session_Namespace('connexion');
            $this->_info = $this->_obj->chargeUser($this->_login->nameUser);
        }
        else
            $this->_info = $this->_obj->chargeUser($this->_login);
        $this->_obj = new infoUser();
        if (!empty($this->_info[0]['NomUtilisateur']))
            $this->_obj->login = $this->_info[0]['NomUtilisateur'];
        if (!empty($this->_info[0]['Mail']))
            $this->_obj->mail = $this->_info[0]['Mail'];
        if ($this->_info[0]['IdDroit'] == '1')
            $this->_obj->droit = 'admin';
        else
            $this->_obj->droit = 'utilisateur';
        $this->_obj->lang = $this->_info[0]['Langue'];
        $this->_obj->listLang = $this->makeListLang();
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function makeListLang($echo = false)
    {
        $h = opendir("./application/configuration/langage/");
        while (($f = readdir($h)) !== false)
        {
            if (@preg_match('/.*\.ini$/', $f) == '1')
                $this->_listLang .= strtolower(substr_replace($f, "", '-4') . ';');
        }
        closedir($h);
        if ($echo == false)
            return $this->_listLang;
        else
        {
            $this->_obj = new infoUser();
            $this->_obj->listLang = $this->_listLang;
            echo Zend_Json_Encoder::encode($this->_obj);
        }
    }

    public function updateUser()
    {
        $this->_login = $this->_controller->getRequest()->getParam('login');
        $this->_pass = $this->_controller->getRequest()->getParam('pass');
        $this->_email = $this->_controller->getRequest()->getParam('email');
        $this->_droit = $this->_controller->getRequest()->getParam('droit');
        $this->_descDroit = $this->_controller->getRequest()->getParam('list');
        $this->_lang = $this->_controller->getRequest()->getParam('lang');
        $this->_login = strtolower($this->_login);
        $this->_droit = strtolower($this->_droit);
        if (!empty($this->_pass))
            $this->_pass = $this->EncryptedPassword($this->_pass);
        $this->_obj = new ObjectUtilisateur;
        $this->_id = $this->_obj->chargeUser($this->_login);
        if ($this->_droit == 'admin' || $this->_droit == 'administrateur')
            $this->_info = $this->_obj->modifUtilisateur($this->_id[0]['IdUtilisateur'], $this->_login, $this->_pass, $this->_email, '1', $this->_lang);
        else
            $this->_info = $this->_obj->modifUtilisateur($this->_id[0]['IdUtilisateur'], $this->_login, $this->_pass, $this->_email, $this->_droit, $this->_lang, $this->_descDroit);
    }

    public function delListDroit()
    {
        $this->_descDroit = $this->_controller->getRequest()->getParam('desc');
        $this->_obj = new ObjectUtilisateur;
        $this->_obj->delDroit($this->_descDroit);
    }

    public function infoNetwork()
    {
        $this->_obj = new ObjectUtilisateur;
        $a = $this->_obj->listeComptesReseaux();
        $this->_obj = new listeNetwork();
        $this->_obj->list = $a;
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function updateNetwork()
    {
        $this->_network = $this->_controller->getRequest()->getParam('name');
        $this->_login = $this->_controller->getRequest()->getParam('login');
        $this->_pass = $this->_controller->getRequest()->getParam('pass');
        $this->_pass .= ',' . $this->_controller->getRequest()->getParam('confpass');
        $this->_obj = new ObjectUtilisateur;
        $this->_obj->ajoutComptesReseaux($this->_network, $this->_login, $this->_pass);
    }

    public function delNetwork()
    {
        $this->_network = $this->_controller->getRequest()->getParam('name');
        $this->_obj = new ObjectUtilisateur;
        $this->_obj->supprimeCompteReseaux($this->_network);
    }

    public function delUser()
    {
        $this->_login = $this->_controller->getRequest()->getParam('list');
        $this->_login = explode(',', $this->_login);
        $this->_obj = new ObjectUtilisateur;
        foreach ($this->_login as $key)
        {
            if (!empty($key))
                $this->_obj->supprimerUtilisateur($key);
        }
    }

    public function listUser()
    {
        $this->_obj = new ObjectUtilisateur;
        $arr = $this->_obj->listeUtilisateurs();
        $this->_login = new listeUser();
        $this->_login->list = $arr;
        echo Zend_Json_Encoder::encode($this->_login);
    }

    private function makeListRight()
    {
        $session = new zend_session_namespace('session');
        $f = './application/configuration/menu/menu_' . $session->lang . '.ini';
        $r = fopen($f, 'r');
        $c = fread($r, filesize($f));
        fclose($r);
        $json = Zend_Json::decode($c);
        $i = 0;
        $a = array();
        while (isset($json['menu'][$i]))
        {
            $j = 0;
            $sMenu = $json['menu'][$i]["item"]["sItem"];
            while (isset($sMenu[$j]))
            {
                if (stristr($sMenu[$j]["value"], 'Deconnexion') != true)
                    $a[$json['menu'][$i]["item"]["titre"]][] = $sMenu[$j]["value"] . ';' . $sMenu[$j]["right"];
                $j++;
            }
            $i++;
        }
        return $a;
    }

    public function listDroits()
    {
        $this->_obj = new ObjectUtilisateur;
        $a = $this->_obj->listeDroits();
        $b = $this->makeListRight();
        $this->_droit = new listeDroit();
        $this->_droit->description = explode(';', $a['description']);
        $this->_droit->list = $b;
        echo Zend_Json_Encoder::encode($this->_droit);
    }

    public function infoDroit()
    {
        $this->_descDroit = $this->_controller->getRequest()->getParam('desc');
        $this->_obj = new ObjectUtilisateur;
        $a = $this->_obj->infoDroit($this->_descDroit);
        $this->_obj = new infoDroit();
        $this->_obj->list = explode(';', $a);
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function infoDroitUtilisateur()
    {
        $this->_login = $this->_controller->getRequest()->getParam('login');
        $this->_login = strtolower($this->_login);
        $this->_obj = new ObjectUtilisateur;
        $this->_idDroit = $this->_obj->chargeUser($this->_login);
        $a = $this->_obj->infoDroitUtilisateur($this->_idDroit[0]['IdDroit']);
        $this->_obj = new infoDroit();
        $this->_obj->list = explode(';', $a[0]['Compo']);
        $this->_obj->desc = $a[0]['Description'];
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function getUserSession()
    {
        $user = new zend_session_namespace('connexion', true);
        $user->idClient = uniqid();
        $user->nameUser = $this->_user;
        $user->idUser = $this->_id;
    }

    public function killUserSession()
    {
        $this->_user = new zend_session_namespace('connexion');
        $this->_session = new zend_session_namespace('session');
        $this->_interface = new zend_session_namespace('interface');

        $this->_user->unsetAll();
        $this->_session->unsetAll();
        $this->_interface->unsetAll();
    }

}

?>
